package com.mybatis_stu.controller;

import com.mybatis_stu.pojo.*;
import com.mybatis_stu.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/classes")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     *新增班级
     * */
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
            clazzService.add(clazz);
            return Result.success();
    }
    /**
     *批量删除
     * */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
            clazzService.deleteByIds(ids);
            return Result.success();
    }
    /**
     *查询班级列表
     * */
    @GetMapping
    public Result list() {
            List<Clazz> classes = clazzService.list();
            return Result.success(classes);
    }
    /**
     *根据职位Id查询班主任列表
     * */
    @GetMapping("/teachers/{positionId}")
    public Result getTeachers(@PathVariable Integer positionId) {
            List<ClassResponse> teachers = clazzService.getTeachersByPositionId(positionId);
            return Result.success(teachers);
    }

    /**
     *带条件的分页查询
     * */

    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志
        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize,name,  begin, end);
        //调用业务层分页查询功能
        PageBean pageBean = clazzService.page(page, pageSize, name,  begin, end);
        //响应
        return Result.success(pageBean);
    }
    /**
     *修改班级
     * */

    @PutMapping
    public Result updateInfo(@RequestBody Clazz clazz) {
        clazzService.updateInfo(clazz);
        return Result.success();
    }
}